/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.wb.swt.SWTResourceManager;
import org.pircbotx.User;

import com.google.code.cubeirc.base.BaseTab;
import com.google.code.cubeirc.common.ApplicationInfo;
import com.google.code.cubeirc.connection.Connection;
import com.google.code.cubeirc.connection.MessagesFormat;
import com.google.code.cubeirc.connection.data.GenericUserResponse;
import com.google.code.cubeirc.connection.data.PrivateMessageResponse;
import com.google.code.cubeirc.dialogs.ColorsChooserAdapter;
import com.google.code.cubeirc.editor.EditorManager;
import com.google.code.cubeirc.editor.HistoryAdapter;
import com.google.code.cubeirc.queue.MessageQueue;
import com.google.code.cubeirc.queue.MessageQueueEnum;
import com.google.code.cubeirc.queue.MessageQueueEvent;
import com.google.code.cubeirc.scripting.ScriptManager;

public class PrivateMessageForm extends BaseTab {


	@Getter @Setter
	private EditorManager editor;
	@Getter @Setter
	private EditorManager inputEditor;
	private StyledText tMessage;

	@Getter @Setter
	private User user;

	public PrivateMessageForm(Composite composite, int style, String name, User user) {
		super(composite, SWT.BORDER, name);
		setUser(user);
		setLayout(new FormLayout());

		StyledText stOutput = new StyledText(this,  SWT.BORDER | SWT.V_SCROLL | SWT.WRAP | SWT.MULTI | SWT.READ_ONLY);
		FormData fd_stOutput = new FormData();
		fd_stOutput.left = new FormAttachment(0);
		stOutput.setLayoutData(fd_stOutput);

		Group group = new Group(this, SWT.NONE);
		fd_stOutput.top = new FormAttachment(group, 6);
		fd_stOutput.right = new FormAttachment(group, 0, SWT.RIGHT);
		FormData fd_group = new FormData();
		fd_group.left = new FormAttachment(0);
		fd_group.right = new FormAttachment(100);
		fd_group.bottom = new FormAttachment(0, 37);
		fd_group.top = new FormAttachment(0);
		group.setLayoutData(fd_group);

		tMessage = new StyledText(this, SWT.BORDER | SWT.SINGLE);
		tMessage.addKeyListener(new ColorsChooserAdapter(getInputEditor()));
		tMessage.addKeyListener(new HistoryAdapter());
		tMessage.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.keyCode == 13)
				{
					String cmd = tMessage.getText();
					if (cmd.startsWith("/"))
					{
						ScriptManager.ParseCmd(tMessage.getText());
						tMessage.setText("");
					}
					else
					{
						MessageQueue.addQueue(MessageQueueEnum.MSG_PRIVATE_OUT, new PrivateMessageResponse(Connection.getUserInfo(), getUser(), tMessage.getText()));
						tMessage.setText("");
					}
				}								
			}
		});
		fd_stOutput.bottom = new FormAttachment(tMessage, -6);
		FormData fd_tMessage = new FormData();
		fd_tMessage.left = new FormAttachment(0);
		fd_tMessage.bottom = new FormAttachment(100, -4);
		tMessage.setLayoutData(fd_tMessage);

		Button btnSend = new Button(this, SWT.FLAT);
		btnSend.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				MessageQueue.addQueue(MessageQueueEnum.MSG_PRIVATE_OUT, new PrivateMessageResponse(Connection.getUserInfo(), getUser(), tMessage.getText()));
				tMessage.setText("");
			}
		});
		fd_tMessage.right = new FormAttachment(btnSend, -6);
		btnSend.setImage(SWTResourceManager.getImage(PrivateMessageForm.class, "/com/google/code/cubeirc/resources/img_send.png"));
		FormData fd_btnSend = new FormData();
		fd_btnSend.right = new FormAttachment(100);
		fd_btnSend.top = new FormAttachment(tMessage, -3, SWT.TOP);
		btnSend.setLayoutData(fd_btnSend);
		btnSend.setText("Send");

		setEditor(new EditorManager(ApplicationInfo.EDT_PRIVATEMSG_NAME, stOutput));
		setInputEditor(new EditorManager(ApplicationInfo.EDT_INPUTCONSOLE_NAME, tMessage));
		getInputEditor().setEditable(true);
	}

	@Override
	public void actionPerformed(MessageQueueEvent e) {
		if (e.getMsgtype() == MessageQueueEnum.MSG_PRIVATE_IN || e.getMsgtype() == MessageQueueEnum.MSG_PRIVATE_OUT)
		{
			PrivateMessageResponse pmr = (PrivateMessageResponse) e.getData();

			//if user message
			if (getUser().equals(pmr.getSender()))				
				getEditor().addText(ApplicationInfo.CLS_COLOR_USERMSG,String.format(MessagesFormat.MSG_USER, pmr.getSender().getNick(),pmr.getMessage()));  


			//If my message
			if (getUser().equals(pmr.getDestination()) && Connection.getUserInfo().equals(pmr.getSender()))
				getEditor().addText(ApplicationInfo.CLS_COLOR_USERMSG,String.format(MessagesFormat.MSG_USER, pmr.getSender().getNick(),pmr.getMessage()));  


		}

		if (e.getMsgtype() == MessageQueueEnum.GLOBAL)
		{		
			getEditor().addText(ApplicationInfo.CLS_COLOR_GLOBAL,String.format(MessagesFormat.MSG_GLOBAL, e.getData().toString()));  
		}

		if (e.getMsgtype() == MessageQueueEnum.IRC_NOTICE)
		{
			GenericUserResponse gur = (GenericUserResponse) e.getData();

			if (gur.getSender().equals(getUser()))
			{
				getEditor().addText(ApplicationInfo.CLS_COLOR_NOTICE,String.format(MessagesFormat.MSG_NOTICE, gur.getSender().getNick(), gur.getText()));  			
			}
		}

		super.actionPerformed(e);
	}

	@Override
	protected void checkSubclass() {

	}
}
